package com.molyfun.parents.activity.store.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.TeacherViewer;
import com.molyfun.parents.activity.coursecenter.ProductDetailActivity;
import com.molyfun.parents.activity.coursecenter.model.SubjectListResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ProductView extends LinearLayout implements View.OnClickListener, View.OnTouchListener {

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.product_name)
    TextView mProductName;
    @InjectView(R.id.price_now)
    TextView mPriceNow;
    @InjectView(R.id.product_time_range)
    TextView mProductTimeRange;
    @InjectView(R.id.people_study_count)
    TextView mStudyCount;
    @InjectView(R.id.teacher_viewer)
    TeacherViewer mTeacherViewer;
    @InjectView(R.id.zhibo_state)
    ImageView mZhiboState;

    private Context context;
    private SubjectListResponse.SubjectBrief mSubjectBrief;

    public ProductView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ProductView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ProductView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_product, this);
        ButterKnife.inject(this);
    }

    public void initData(SubjectListResponse.SubjectBrief subjectBrief) {
        if (subjectBrief == null) {
            return;
        }
        mSubjectBrief = subjectBrief;
        mProductName.setText(subjectBrief.getName());
        mPriceNow.setText(subjectBrief.getPrice() + "");
        mStudyCount.setText(subjectBrief.getSellcount() + "");
        mProductTimeRange.setVisibility(View.GONE);
        if ((subjectBrief.getBegindatetime() == 0l) && (subjectBrief.getEnddatetime() == 0l)) {
            mProductTimeRange.setText("多媒体课" + subjectBrief.getMedianum() + "节");
        } else {
            mProductTimeRange.setVisibility(View.VISIBLE);
            mProductTimeRange.setText("开课时间:" + TimeUtils.getDateByTimeMillis(subjectBrief.getBegindatetime()) + "~" + TimeUtils.getDateByTimeMillis(subjectBrief.getEnddatetime()));
        }
        mTeacherViewer.initData(subjectBrief.getUserlist());
        setOnClickListener(this);
        mTeacherViewer.setOnTouchListener(this);
        if (!TextUtils.isEmpty(subjectBrief.getLivestatus()) && subjectBrief.getLivestatus().equals("prog")) {
            mZhiboState.setVisibility(View.VISIBLE);
        } else {
            mZhiboState.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN://0
                return false;
            case MotionEvent.ACTION_UP:
                ProductDetailActivity.startActivity(getContext(), mSubjectBrief.getId(), PreferencesStorageUtil.getStudentId(getContext()), Const.CHATROWPRODUCT);
                return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                ProductDetailActivity.startActivity(getContext(), mSubjectBrief.getId(), PreferencesStorageUtil.getStudentId(getContext()), Const.CHATROWPRODUCT);
                break;
        }
    }
}